package cn.findviewbydream.evil.main.service.impl;

import cn.findviewbydream.evil.main.dao.SettingBaseDAO;
import cn.findviewbydream.evil.main.entity.BaseEssentialInfoEntity;
import cn.findviewbydream.evil.main.entity.BaseSeatEntity;
import cn.findviewbydream.evil.main.exception.UploadImgException;
import cn.findviewbydream.evil.main.service.SettingBaseService;
import cn.findviewbydream.evil.main.utils.FileWrite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.List;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.main.service.impl
 * 时间：17-2-21
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
@Service
public class SettingBaseServiceImpl implements SettingBaseService
{
    private static final String[] TYPE_LIST = {"大型酒店", "中型饭店", "西餐厅", "小型餐厅", "家庭式餐厅"};

    private final SettingBaseDAO settingBaseDAO;

    @Autowired
    public SettingBaseServiceImpl(SettingBaseDAO settingBaseDAO) {this.settingBaseDAO = settingBaseDAO;}

    @Override
    public String[] getTypeList()
    {
        return TYPE_LIST;
    }

    @Override
    public BaseEssentialInfoEntity getBaseEssentialInfo()
    {
        return settingBaseDAO.selectBaseEssentialInfo();
    }

    @Override
    public List<BaseSeatEntity> getBaseSeatList()
    {
        return settingBaseDAO.selectBaseSeatList();
    }

    @Override
    public BaseSeatEntity getBaseSeatById(String id)
    {
        return settingBaseDAO.selectBaseSeatById(id);
    }

    @Override
    public int getBaseSeatLastId()
    {
        return settingBaseDAO.selectBaseSeatLastId();
    }

    @Override
    public boolean postBaseSeat(int id, String seat, boolean state)
    {
        return settingBaseDAO.replaceBaseSeat(new BaseSeatEntity(id, seat, state)) == 1;
    }

    @Override
    public boolean postBaseSeatState(int id, boolean state)
    {
        BaseSeatEntity entity = new BaseSeatEntity();
        entity.setSeatId(id);
        entity.setState(state);
        return settingBaseDAO.updateBaseSeatState(entity) == 1;
    }

    @Override
    public boolean postEssentialSubmit(String name, String address, String introduction, String type,
                                       CommonsMultipartFile img)
    {
        BaseEssentialInfoEntity entity = new BaseEssentialInfoEntity();

        if (img != null)
        {
            String filePath = System.getProperty("root.path") + "static/upload/img/essential";

            try
            {
                String imgName = img.getOriginalFilename();
                imgName = imgName.replace(imgName.substring(0, imgName.lastIndexOf(".")), "essential");
                FileWrite.write(img, filePath, imgName);
                entity.setImg(imgName);
            }
            catch (IOException e)
            {
                throw new UploadImgException("图片上传失败！", e);
            }
        }

        entity.setId(1);
        entity.setName(name);
        entity.setAddress(address);
        entity.setIntroduction(introduction);
        entity.setType(type);

        return settingBaseDAO.replaceBaseEssentialInfo(entity) > 0;
    }

}
